package gui.settings;

import etc.Utils;
import java.io.File;
import java.util.HashMap;
import javax.swing.JOptionPane;

public class SettingsController {	

	/**
	 * Shows a modal <code>SettingsPanel</code>.
	 * <br>
	 * If a parameter is given, tries to load settings in them to SettingsPanel objects.
	 * Else, the SettingsPanel will be shown with empty inputs.
	 * @param settingsMap <code>HashMap</code> to load parameters from
	 * @return If the dialog was canceled by user or any parameter is invalid: NULL,
	 * else the input from user.
	 */
	static public HashMap<String, Object> showSettingsDialog( HashMap<String, Object> settingsMap ) {
		SettingsPanel sp = createSettingsPanel(settingsMap);
		
		int dialogResult =
			JOptionPane.showConfirmDialog(
				null,
				sp,
				"Connection Settings",
				JOptionPane.OK_CANCEL_OPTION,
				JOptionPane.QUESTION_MESSAGE
			);

		if( dialogResult == JOptionPane.OK_OPTION ) {
			String localFolderPath = sp.getLocalFolderPath();
			if( localFolderPath == null ) {
				showWarning("You did not provide a local synchronization folder.");
				return null;
			}
			File localFolder = new File(localFolderPath);
			if( localFolder == null ) {
				showWarning("Program could not open the local synchronization folder you provided.");
				return null;
			}

			String remoteIP = sp.getRemoteIP();
			if( !Utils.isValidIP(remoteIP) ) {
				showWarning("You did not provide a valid remote IP address.");
				return null;
			}

			HashMap<String, Object> userSettingsMap = getSettings(sp);
			return userSettingsMap;
		}

		return null;
	}

	static private void showWarning( String warningMessage ) {
		JOptionPane.showMessageDialog(
			null,
			warningMessage + "\nSettings will not be saved.",
			"Warning",
			JOptionPane.WARNING_MESSAGE
		);
	}

	static private SettingsPanel createSettingsPanel( HashMap<String, Object> settingsMap ) {
		SettingsPanel sp = new SettingsPanel();

		if( settingsMap != null ) {
			if( settingsMap.containsKey("LOCAL_USERNAME") ) {
				Object temp = settingsMap.get("LOCAL_USERNAME");
				if( temp instanceof String ) sp.setLocalUsername( (String)temp );
			}
			if( settingsMap.containsKey("LOCAL_PASSWORD") ) {
				Object temp = settingsMap.get("LOCAL_PASSWORD");
				if( temp instanceof String ) sp.setLocalPassword( (String)temp );
			}
			if( settingsMap.containsKey("LOCAL_PATH") ) {
				Object temp = settingsMap.get("LOCAL_PATH");
				if( temp instanceof String )sp.setLocalFolderPath( (String)temp );
			}
			if( settingsMap.containsKey("LOCAL_PORT") ) {
				Object temp = settingsMap.get("LOCAL_PORT");
				if( temp instanceof Integer )sp.setLocalPort( (Integer)temp );
			}

			if( settingsMap.containsKey("REMOTE_IP") ) {
				Object temp = settingsMap.get("REMOTE_IP");
				if( temp instanceof String )sp.setRemoteIP( (String)temp );
			}
			if( settingsMap.containsKey("REMOTE_PORT") ) {
				Object temp = settingsMap.get("REMOTE_PORT");
				if( temp instanceof Integer )sp.setRemotePort( (Integer)temp );
			}
			if( settingsMap.containsKey("REMOTE_USERNAME") ) {
				Object temp = settingsMap.get("REMOTE_USERNAME");
				if( temp instanceof String )sp.setRemoteUsername( (String)temp );
			}
			if( settingsMap.containsKey("REMOTE_PASSWORD") ) {
				Object temp = settingsMap.get("REMOTE_PASSWORD");
				if( temp instanceof String )sp.setRemotePassword( (String)temp );
			}
		}

		return sp;
	}

	static private HashMap<String, Object> getSettings( SettingsPanel sp ) {
		if( sp == null ) return null;
		
		HashMap<String, Object> retmap = new HashMap<String, Object>(9);

		retmap.put( "LOCAL_USERNAME", sp.getLocalUsername() );
		retmap.put( "LOCAL_PASSWORD", sp.getLocalPassword() );
		retmap.put( "LOCAL_PORT", sp.getLocalPort() );
		retmap.put( "LOCAL_PATH", sp.getLocalFolderPath() );

		retmap.put( "REMOTE_USERNAME", sp.getRemoteUsername() );
		retmap.put( "REMOTE_PASSWORD", sp.getRemotePassword() );
		retmap.put( "REMOTE_IP", sp.getRemoteIP() );
		retmap.put( "REMOTE_PORT", sp.getRemotePort() );

		return retmap;
	}

}
